package com.pdg.islandinterloper.clientstatehandlers;

import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.pdg.islandinterloper.MainActivity;
import com.pdg.islandinterloper.R;
import com.pdg.islandinterloper.baseclasses.ClientStateHandlerBase;
import com.pdg.islandinterloper.database.objects.Profile;
import com.pdg.islandinterloper.enums.ClientState;

public class UninitializeProfileClientStateHandler  extends ClientStateHandlerBase<MainActivity> implements OnSeekBarChangeListener  {

	public UninitializeProfileClientStateHandler(MainActivity theActivity) {
		super(theActivity);
	}
	@Override
	public void handleHide() {
	}
	@Override
	public void handleButtonClick(View theView) {
		switch(theView.getId()){
		case R.id.button_back:
			getActivity().setState(ClientState.PLAY);
			break;
		case R.id.button_create_world:
			createWorld();
			break;
		}
	}
	private void createWorld() {
		Profile theProfile = getProfile();
		theProfile.setIslandView(getSliderValue(R.id.slider_island_view,Profile.getMinimumIslandView()));
		theProfile.setMaximumTurns(getSliderValue(R.id.slider_maximum_turns,Profile.getMinimumMaximumTurns()));
		theProfile.setTurnInterval(getSliderValue(R.id.slider_turn_interval,Profile.getMinimumTurnInterval()));
		theProfile.setWorldHeight(getSliderValue(R.id.slider_world_height,Profile.getMinimumWorldHeight()));
		theProfile.setWorldWidth(getSliderValue(R.id.slider_world_width,Profile.getMinimumWorldWidth()));
		theProfile.setWorldWrapX(getCheckBoxValue(R.id.checkbox_world_wrap_x));
		theProfile.setWorldWrapY(getCheckBoxValue(R.id.checkbox_world_wrap_y));
		theProfile.setIslandCount(getSliderValue(R.id.slider_island_count,Profile.getMinimumIslandCount()));
		theProfile.setIslandSpacing(getSliderValue(R.id.slider_island_spacing,Profile.getMinimumIslandSpacing()));
		getActivity().getDatabase().getProfileTableProvider().updateProfile(theProfile);
		getActivity().setState(ClientState.CREATE_WORLD);
		
	}
	private Boolean getCheckBoxValue(int theCheckBoxId) {
		CheckBox theCheckBox = (CheckBox)getActivity().findViewById(theCheckBoxId);
		return theCheckBox.isChecked();
	}
	private Integer getSliderValue(int theSliderId,
			Integer theMinimum) {
		SeekBar theSeekBar = (SeekBar)getActivity().findViewById(theSliderId);
		return theSeekBar.getProgress()+theMinimum;
	}
	@Override
	public boolean handleMenu(MenuItem theItem) {
		return false;
	}
	@Override
	public void handleShow() {
		initializeControls();
	}
	private void initializeControls() {
		initializeWorldWidthControls();
		initializeWorldHeightControls();
		initializeWorldWrapControls();
		initializeIslandCountControls();
		initializeIslandSpacingControls();
		initializeIslandViewControls();
		initializeMaximumTurnControls();
		initializeTurnIntervalControls();
	}
	private void initializeTurnIntervalControls() {
		setSliderAndLabelValue(R.id.slider_maximum_turns,R.id.label_maximum_turns_value,Profile.getMinimumMaximumTurns(),Profile.getMaximumMaximumTurns(),getProfile().getMaximumTurns());
	}
	private void initializeMaximumTurnControls() {
		setSliderAndLabelValue(R.id.slider_turn_interval,R.id.label_turn_interval_value,Profile.getMinimumTurnInterval(),Profile.getMaximumTurnInterval(),getProfile().getTurnInterval());
	}
	private void setSliderAndLabelValue(int theSliderId,int theTextViewId,int theMinimum,int theMaximum,int theValue){
		SeekBar theSlider = (SeekBar)getActivity().findViewById(theSliderId);
		theSlider.setOnSeekBarChangeListener(this);
		theSlider.setMax(theMaximum-theMinimum);
		theSlider.setProgress(theValue-theMinimum);
		TextView theLabel = (TextView)getActivity().findViewById(theTextViewId);
		theLabel.setText(String.format(getActivity().getResources().getString(R.string.format_integer),theValue));
	}
	private void initializeIslandViewControls() {
		setSliderAndLabelValue(R.id.slider_island_view,R.id.label_island_view_value,Profile.getMinimumIslandView(),Profile.getMaximumIslandView(),getProfile().getIslandView());
	}
	private void initializeIslandSpacingControls() {
		setSliderAndLabelValue(R.id.slider_island_spacing,R.id.label_island_spacing_value,Profile.getMinimumIslandSpacing(),Profile.getMaximumIslandSpacing(),Profile.getDefaultIslandSpacing());
	}
	private void initializeIslandCountControls() {
		setSliderAndLabelValue(R.id.slider_island_count,R.id.label_island_count_value,Profile.getMinimumIslandCount(),Profile.getMaximumIslandCount(),Profile.getDefaultIslandCount());
	}
	private void initializeWorldWrapControls() {
		CheckBox theCheckBox = (CheckBox)getActivity().findViewById(R.id.checkbox_world_wrap_x);
		theCheckBox.setChecked(getProfile().getWorldWrapX());
		theCheckBox = (CheckBox)getActivity().findViewById(R.id.checkbox_world_wrap_y);
		theCheckBox.setChecked(getProfile().getWorldWrapY());
	}
	private void initializeWorldHeightControls() {
		setSliderAndLabelValue(R.id.slider_world_height,R.id.label_world_height_value,Profile.getMinimumWorldHeight(),Profile.getMaximumWorldHeight(),getProfile().getWorldHeight());
	}
	private void initializeWorldWidthControls() {
		setSliderAndLabelValue(R.id.slider_world_width,R.id.label_world_width_value,Profile.getMinimumWorldWidth(),Profile.getMaximumWorldWidth(),getProfile().getWorldWidth());
	}
	private Profile getProfile() {
		return getActivity().getDatabase().getProfileTableProvider().getProfileById(getActivity().getProfileId());
	}
	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		int theTextViewId;
		int theValue;
		switch(seekBar.getId()){
		case R.id.slider_world_width:
			theTextViewId = R.id.label_world_width_value;
			theValue = Profile.getMinimumWorldWidth()+progress;
			break;
		case R.id.slider_world_height:
			theTextViewId = R.id.label_world_height_value;
			theValue = Profile.getMinimumWorldHeight()+progress;
			break;
		case R.id.slider_island_count:
			theTextViewId = R.id.label_island_count_value;
			theValue = Profile.getMinimumIslandCount()+progress;
			break;
		case R.id.slider_island_spacing:
			theTextViewId = R.id.label_island_spacing_value;
			theValue = Profile.getMinimumIslandSpacing()+progress;
			break;
		case R.id.slider_island_view:
			theTextViewId = R.id.label_island_view_value;
			theValue = Profile.getMinimumIslandView()+progress;
			break;
		case R.id.slider_maximum_turns:
			theTextViewId = R.id.label_maximum_turns_value;
			theValue = Profile.getMinimumMaximumTurns()+progress;
			break;
		case R.id.slider_turn_interval:
			theTextViewId = R.id.label_turn_interval_value;
			theValue = Profile.getMinimumTurnInterval()+progress;
			break;
		default:
			return;
		}
		TextView theLabel = (TextView)getActivity().findViewById(theTextViewId);
		theLabel.setText(String.format(getActivity().getResources().getString(R.string.format_integer),theValue));
	}
	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
	}
	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
	}
}
